package com.ccjk.beusoft.hot.plugin

import android.app.Activity
import android.content.Intent
import android.content.res.AssetManager
import android.content.res.Resources
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.*

/**
 * Created by CCJK on 2017/12/2.
 */
class HostProxyActivity : AppCompatActivity(), Bind {


    private  var activityProxy: ActivityProxy?=null
    private  var pluginActivity: PluginActivityInterface?=null

    //
    override fun bind(activity: Activity, pluginPackage: PluginPackage) {
        pluginActivity = activity as PluginActivityInterface

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityProxy = ActivityProxy(this)
        activityProxy?.onCreate(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        pluginActivity?.onDestroy()
    }


    override fun onResume() {
        super.onResume()
        pluginActivity?.onResume()
    }

    override fun onPause() {
        super.onPause()
        pluginActivity?.onPause()
    }

    override fun onRestart() {
        super.onRestart()
        pluginActivity?.onRestart()
    }

    override fun onStart() {
        super.onStart()
        pluginActivity?.onStart()
    }

    override fun onStop() {
        super.onStop()
        pluginActivity?.onStop()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        pluginActivity?.onActivityResult(requestCode, resultCode, data)
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        pluginActivity?.onSaveInstanceState(outState)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        pluginActivity?.onRestoreInstanceState(savedInstanceState)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        pluginActivity?.onNewIntent(intent)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return pluginActivity?.onTouchEvent(event)?:false || super.onTouchEvent(event)
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        return pluginActivity?.onKeyUp(keyCode, event)?:false || super.onKeyUp(keyCode, event)
    }

    override fun onWindowAttributesChanged(params: WindowManager.LayoutParams?) {
        super.onWindowAttributesChanged(params)
        pluginActivity?.onWindowAttributesChanged(params)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        pluginActivity?.onWindowFocusChanged(hasFocus)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        pluginActivity?.onBackPressed()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        return pluginActivity?.onCreateOptionsMenu(menu)?:false || super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return pluginActivity?.onOptionsItemSelected(item)?:false || super.onOptionsItemSelected(item)
    }

    override fun getAssets(): AssetManager {
        return if (activityProxy?.getPluginAssetManager() == null) super.getAssets() else activityProxy!!.getPluginAssetManager()!!
    }

    override fun getResources(): Resources {

        return if (activityProxy?.getPluginResource() == null) super.getResources() else activityProxy!!.getPluginResource()!!
    }

}